﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using RWLib;

namespace RW_GUI
{
    /// <summary>
    /// Interaction logic for FluentChoiceWindow.xaml
    /// </summary>
    public partial class FluentChoiceWindow : Window
    {
        public string Name;
        private List<Fluent> _fluents;
        private bool addOperator;
        public FluentChoiceWindow(List<Fluent> fluents, bool isNullOrEmpty)
        {
            _fluents = fluents;
            addOperator = !isNullOrEmpty;
            InitializeComponent();
            InitializeButtons();
        }
        public void InitializeButtons()
        {
            Grid_Buttons.Children.Clear();
            var buttons = new List<Button>();
            var squareroot = (int)Math.Ceiling(Math.Sqrt(_fluents.Count));
            foreach (var fluent in _fluents)
            {
                var button = new Button();
                button.Click += FluentButtonClick;
                Grid_Buttons.ColumnDefinitions.Clear();
                Grid_Buttons.RowDefinitions.Clear();
                for (var i = 0; i < squareroot; i++)
                {
                    Grid_Buttons.RowDefinitions.Add(new RowDefinition { Height = new GridLength(Grid_Buttons.Height / _fluents.Count, GridUnitType.Star) });
                }
                for (var i = 0; i < squareroot; i++)
                {
                    Grid_Buttons.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(squareroot, GridUnitType.Star) });
                }
                button.Content = fluent.Name;
                buttons.Add(button);
            }
            var j = 0;
            var row = 0;
            foreach (var b in buttons)
            {
                Grid.SetRow(b, row);
                Grid.SetColumn(b, j);
                Grid_Buttons.Children.Add(b);
                j++;
                if (j < squareroot) continue;
                j = 0;
                row++;
            }
            Grid_Buttons.UpdateLayout();
        }
        private void FluentButtonClick(object sender, RoutedEventArgs e)
        {
            if (addOperator) Name = Radio_Operator.IsChecked.Value ? " and " : " or ";
            if (!Chk_True.IsChecked.Value) Name += "not ";
            Name += (string)((Button)sender).Content;
            Close();
        }

        private void Chk_True_Click(object sender, RoutedEventArgs e)
        {
            Chk_True.Content = Chk_True.IsChecked != null && Chk_True.IsChecked.Value ? "True" : "False";
        }
    }
}
